<!DOCTYPE html>
<html lang="en" xmlns:th="https://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<div th:replace="common/common::html"></div>
<head>
    <meta charset="UTF-8">
    <title th:text="#{user.page.title}">Title</title>
    <script th:inline="javascript">
        var _gridWidth = null;
        var _gridHeight = null;
        //页面自适应
        function resizePageSize(){
            _gridWidth = $(window).width()-12;/*  -189 是去掉左侧 菜单的宽度，   -12 是防止浏览器缩小页面 出现滚动条 恢复页面时  折行的问题 */
            _gridHeight = $(window).height()-32-75; /* -32 顶部主菜单高度，   -140 查询条件高度*/
        }



        $(function () {
            window.addEventListener("resize",function () {
                resizePageSize();
                $("#flexgrid-id").flexResize(_gridWidth,_gridHeight)
            })
            resizePageSize();
            var _columnWidth = (_gridWidth - 150)/6;
            $("#flexgrid-id").flexigrid({
                width : _gridWidth,
                height : _gridHeight,
                url : /*[[@{queryUserInfoBypage}]]*/"",
                dataType : 'json',
                colModel : [
                    {display : 'userUuid',name : 'userUuid',width : 150,sortable : false,align : 'center',hide : 'true'},
                    {display : /*[[#{login.user.name}]]*/"",name : 'userName',width : _columnWidth, sortable : true,align : 'center'},
                    {display :  /*[[#{user.nice.name}]]*/"",name : 'niceName',width : _columnWidth, sortable : true,align : 'center'},
                    {display :  /*[[#{user.email}]]*/"",name : 'email',width : _columnWidth, sortable : true,align : 'center'},
                    {display :  /*[[#{user.mobile}]]*/"",name : 'mobile',width : _columnWidth, sortable : true,align : 'center'},
                    {display :  /*[[#{common.operation}]]*/"",name : 'userUuid',width : _columnWidth, sortable : true,align : 'center',
                        process: function(v,_trid,_row) {
                        var contents = "";
                            if([[${#authorization.expression("hasRole('ROLE_cldk_data_user_role')")}]])
                            {
                                contents += '<a class="btn title=[[#{user.allow.auth}]] onclick="allowRoleByUser(\'' + v + '\')"> <i class="glyphicon glyphicon-wrench"></i> </a>';
                                contents += '<a class="btn title=[[#{user.allow.reset}]] onclick="resetPasswordByUser(\'' + v + '\')"> <i class="glyphicon glyphicon-lock"></i> </a>';
                            }
                        return contents;
                        }},
                ],
                resizable : false, //resizable table是否可伸缩
                useRp : true,
                usepager : true, //是否分页
                autoload : false, //自动加载，即第一次发起ajax请求
                hideOnSubmit : true, //是否在回调时显示遮盖
                showcheckbox : true, //是否显示多选框
                //rowhandler : rowDbclick, //是否启用行的扩展事情功能,在生成行时绑定事件，如双击，右键等
                rowbinddata : true

            });
            query();
            //查询按钮
            $("#query-btn-id").click(function () {
                query();
            });

            //初始化分配用户对话框
            $("#allow-auth-dialog-id").dialog({
                autoOpen : false,
                width : 400,
                modal : true,
                resizable : false,
                title: /*[[#{user.allow.auth.dialog.title}]]*/'',
                buttons: [
                    {
                        text: /*[[#{common.confirm}]]*/'',
                        click: function() {
                            var roleArray = [];
                            var ztreeObj = $.fn.zTree.getZTreeObj("allow-atuh-ztree-id");
                            var nodes = ztreeObj.getCheckedNodes(true);
                            $(nodes).each(function (index,item) {
                                roleArray.push(item.id);
                            })
                            // console.info(roleArray)
                            var userIds = $("#allow-auth-dialog-id").attr("userIds");
                            $.ajax({
                                type: 'post',
                                async: false,
                                dataType: 'json',
                                url: /*[[@{/user/saveRoleRefUser}]]*/'',
                                data: [
                                    {name: "userIds", value: userIds},
                                    {name: "roleIds", value: roleArray}
                                ],
                                success: function (data) {
                                    message(data.msg);
                                    $("#allow-auth-dialog-id").dialog('close');
                                },
                                error: function (msg) {
                                    message(/*[[#{common.error}]]*/'');
                                }
                            });

                        }
                    },
                    {
                        text: /*[[#{common.cancel}]]*/'',
                        click: function() {
                            $( this ).dialog( "close" );
                        }
                    }
                ]
            });


            //分配角色按钮
            $("#allow-auth-btn-id").click(function () {

                loadZtree();
            });



            //重置密码按钮
            $("#reset-btn-id").click(function () {
                resetPassword();
            });


            //条件查询工具条初始化
            $("#nav-tools-article-id").searchTools({
                colModel : [
                    {display : /*[[#{user.name}]]*/"", domId:"search-user-name"},
                    {display : /*[[#{user.nick}]]*/"",  domId:"search-nice-name"},
                ],
                reback:function () {
                    query();
                }
            });
        });//ready

        function query() {
            //条件查询读取属性
            var userName = $("#search-user-name").attr("select-value");
            var niceName = $("#search-nice-name").attr("select-value");
            $("#flexgrid-id").flexOptions({
                extParam:[
                    {name:"userName", value:userName},
                    {name:"niceName", value:niceName}
                ]
            }).flexReload();
        }
        var allocateSetting = {
            check: {
                enable: true
            },
            data: {
                simpleData: {
                    enable: true
                }
            }
        };

        //批量重置密码
        function resetPassword() {

            var ids = $("#flexgrid-id").getFlexigridiAttrByIndex(0);
            if(ids.length === 0){
                message(/*[[#{common.select.one}]]*/);
                return;
            }
            //初始化重置密码对话框
            $("#reset-password-dialog-id").dialog({
                autoOpen : true,
                width : 400,
                modal : true,
                resizable : false,
                title: /*[[#{user.reset.password.dialog.title}]]*/'',
                buttons: [
                    {
                        text: /*[[#{common.confirm}]]*/'',
                        click: function() {
                            resetPass = $("#reset-password-ids").val();
                            $("#allow-auth-dialog-id").attr("userIds",userId);
                            $.ajax({
                                type: 'post',
                                async: false,
                                dataType : 'json',
                                url: /*[[@{/user/resetPasswordAll}]]*/'',
                                data: [
                                    {name:'userId',value:userId},
                                    {name:'resetPass',value:resetPass}
                                ],
                                success: function (data) {
                                    message(data.msg);
                                },
                                error: function(msg){
                                    message(/*[[#{common.error}]]*/'');
                                }
                            });
                        }
                    },
                    {
                        text: /*[[#{common.cancel}]]*/'',
                        click: function() {
                            $( this ).dialog( "close" );
                        }
                    }
                ]
            });

        }
        //批量分配角色
        function loadZtree() {
            var ids = $("#flexgrid-id").getFlexigridiAttrByIndex(0);
            if(ids.length === 0){
                message(/*[[#{common.select.one}]]*/);
                return;
            }
            $("#allow-auth-dialog-id").attr("userIds",ids);
            $.ajax({
                type: 'post',
                async: false,
                dataType : 'json',
                url: /*[[@{/user/queryRoleZtreeAll}]]*/'',
                data: [],
                success: function (data) {
                    $.fn.zTree.init($("#allow-atuh-ztree-id"), allocateSetting, data);
                    $("#allow-auth-dialog-id").dialog('open');
                },
                error: function(msg){
                    message(/*[[#{common.error}]]*/'');
                }
            });
        }

        //根据用户id重置密码
        function resetPasswordByUser(userId) {
            //初始化重置密码对话框
            $("#reset-password-dialog-id").dialog({
                autoOpen : true,
                width : 400,
                modal : true,
                resizable : false,
                title: /*[[#{user.reset.password.dialog.title}]]*/'',
                buttons: [
                    {
                        text: /*[[#{common.confirm}]]*/'',
                        click: function() {
                            resetPass = $("#reset-password-ids").val();
                            $("#allow-auth-dialog-id").attr("userIds",userId);
                            $.ajax({
                                type: 'post',
                                async: false,
                                dataType : 'json',
                                url: /*[[@{/user/resetPasswordByUser}]]*/'',
                                data: [
                                    {name:'userId',value:userId},
                                    {name:'resetPass',value:resetPass}
                                ],
                                success: function (data) {
                                    message(data.msg);
                                    $("#reset-password-dialog-id").dialog('close');
                                },
                                error: function(msg){
                                    message(/*[[#{common.error}]]*/'');
                                }
                            });
                        }
                    },
                    {
                        text: /*[[#{common.cancel}]]*/'',
                        click: function() {
                            $( this ).dialog( "close" );
                        }
                    }
                ]
            });


        }
        //根据用户id分配角色
        function allowRoleByUser(userId) {
            $("#allow-auth-dialog-id").attr("userIds",userId);
            $.ajax({
                type: 'post',
                async: false,
                dataType : 'json',
                url: /*[[@{/user/queryRoleZtreeByUser}]]*/'',
                data: [
                    {name:'userId',value:userId}
                ],
                success: function (data) {
                    $.fn.zTree.init($("#allow-atuh-ztree-id"), allocateSetting, data);
                    $("#allow-auth-dialog-id").dialog('open');
                },
                error: function(msg){
                    message(/*[[#{common.error}]]*/'');
                }
            });
        }
    </script>

</head>
<body style="display: flex;flex-direction: column;">
<nav style="flex-basis: 31px;display: flex;flex-direction: row;margin:5px 0">
    <article id="nav-tools-article-id" class="nav-tools-article"></article>
    <nav style="flex-grow: 1">
        <a id="query-btn-id" class="btn" th:title="#{common.query}">
            <i class="glyphicon glyphicon-search"></i>
        </a>

        <a sec:authorize="hasRole('ROLE_cldk_data_user_role')" id="allow-auth-btn-id" class="btn" th:title="#{user.allow.auth}">
            <i class="glyphicon glyphicon-wrench"></i>
        </a>

        <a id="reset-btn-id" class="btn" th:title="#{user.reset.password}">
            <i class="glyphicon glyphicon-lock"></i>
        </a>
    </nav>

</nav>
<table id="flexgrid-id" style="flex-grow: 1"></table>

<div id="allow-auth-dialog-id" style="display: none;">
    <ul id="allow-atuh-ztree-id" class="ztree"></ul>
</div>

<div id="reset-password-dialog-id" style="display: none;">
<!--    <ul id="allow-atuh-ztree-id" class="ztree"></ul>-->
    <div class="form-group">
        <span th:text="#{reset.password.ids}"></span>
        <input id="reset-password-ids" class="form-control" name="password">
    </div>
</div>


</body>
</html>